<?php
// +----------------------------------------------------------------------
// | 众联TK架构 [ ZoomTk ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2037 成都众联思索科技有限公司 [ http://www.zoomtk.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.sopvip.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\mall\admin;
use app\common\builder\TKuitpl;
use app\mall\common\Adminbase;
use think\Model;
use think\Cache;
use think\Db;

/**
 * 商家管理中心
 * @package app\mall\admin
 */
class Seller extends Adminbase
{
    /**
     * 首页
     * @author Sunday <153788231@qq.com>
     * @return mixed
     */
    public function index()
    {
        $model_store = Model('store');
        // 设置页码参数名称
        $condition = array();
        $condition['is_own_shop'] = 0;
        if ($_GET['store_name'] != '') {
            $condition['store_name'] = array('like', '%' . $_GET['store_name'] . '%');
        }
        if ($_GET['member_name'] != '') {
            $condition['member_name'] = array('like', '%' . $_GET['member_name'] . '%');
        }
        if ($_GET['seller_name'] != '') {
            $condition['seller_name'] = array('like', '%' . $_GET['seller_name'] . '%');
        }
        if ($_GET['grade_id'] != '') {
            $condition['grade_id'] = $_GET['grade_id'];
        }
        if ($_GET['store_state'] != '') {
            $condition['store_state'] = $_GET['store_state'];
        }
        if ($_POST['query'] != '') {
            $condition[$_POST['qtype']] = array('like', '%' . $_POST['query'] . '%');
        }
        $order = '';
        $param = array('store_id','store_name','member_name','seller_name','store_time','store_end_time','store_state','grade_id','sc_id');
        if (in_array($_POST['sortname'], $param) && in_array($_POST['sortorder'], array('asc', 'desc'))) {
            $order = $_POST['sortname'] . ' ' . $_POST['sortorder'];
        }
        $page = $_POST['rp'];
        //店铺列表
        $store_list = $model_store->getStoreList($condition, $page, $order,'*',20);
        //店铺等级
        $model_grade = Model('storegrade');
        $grade_list = $model_grade->getGradeList(array());
        $grade_array = array();
        if (!empty($grade_list)){
            foreach ($grade_list as $v){
                $grade_array[$v['sg_id']] = $v['sg_name'];
            }
        }
        //店铺分类
        $model_store_class = Model('storeclass');
        $class_list = $model_store_class->getStoreClassList(array(),'',20);
        $class_array = array();
        if (!empty($class_list)) {
            foreach ($class_list as $v) {
                $class_array[$v['sc_id']] = $v['sc_name'];
            }
        }
        foreach ($store_list as $value) {
            $param = array();
            $store_state = $this->getStoreState($value);
//            $operation = "<a class='btn green' href='index.php?s=store&op=store_joinin_detail&member_id=".$value['member_id']."'><i class='fa fa-list-alt'></i>查看</a><span class='btn'><em><i class='fa fa-cog'></i>" . L('nc_set') . " <i class='arrow'></i></em><ul><li><a href='index.php?s=store&op=store_edit&store_id=" . $value['store_id'] . "'>编辑店铺信息</a></li><li><a href='index.php?s=store&op=store_bind_class&store_id=" . $value['store_id'] . "'>修改经营类目</a></li>";
//            if (str_cut($store_state, 6) == 'expire'  && cookie('remindRenewal'.$value['store_id']) == null) {
//                $operation .= "<li><a class='expire' href=". urlAdminShop('store', 'remind_renewal', array('store_id'=>$value['store_id'])). ">提醒商家续费</a></li>";
//            }
//            $operation .= "</ul></span>";
//            $param['operation'] = $operation;
            $param['store_id'] = $value['store_id'];
            $param['store_name'] = $value['store_name'] ;
            $param['member_id'] = $value['member_name'];
            $param['seller_name'] = $value['seller_name'];
//            $param['store_avatar'] = "<a href='javascript:void(0);' class='pic-thumb-tip' onMouseOut='toolTip()' onMouseOver='toolTip(\"<img src=".getStoreLogo($value['store_avatar']).">\")'><i class='fa fa-picture-o'></i></a>";
//            $param['store_label'] = "<a href='javascript:void(0);' class='pic-thumb-tip' onMouseOut='toolTip()' onMouseOver='toolTip(\"<img src=".getStoreLogo($value['store_label'], 'store_logo').">\")'><i class='fa fa-picture-o'></i></a>";
            $param['grade_id'] = $grade_array[$value['grade_id']];
            $param['store_time'] = date('Y-m-d', $value['store_time']);
            $param['store_end_time'] = $value['store_end_time']?date('Y-m-d', $value['store_end_time']):'无限';
            $param['store_state'] = $value['store_state']?'1':'0';
            $param['sc_id'] = $class_array[$value['sc_id']];
            $param['area_info'] = $value['area_info'];
            $param['store_address'] = $value['store_address'];
            $param['store_qq'] = $value['store_qq'];
            $param['store_ww'] = $value['store_ww'];
            $param['store_phone'] = $value['store_phone'];
            $data[$value['store_id']] = $param;
        }
        $list_tab = [
            'tab1' => ['title' => '管理', 'url' => url('index')],
            'tab2' => ['title' => '入驻审核', 'url' => url('store_joinin')],
        ];
        // 使用TKuitpl快速创建数据表格
        return TKuitpl::make('table')
            ->setPageTitle('供应商管理')// 设置页面标题
            ->setTabNav($list_tab,  'tab1')
            ->setPrimaryKey('member_id') // 设置主键名为username
            ->setSearch(['store_id' => 'ID', 'seller_name' => '商家账号'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['store_id', 'ID'],
                ['member_id', '店铺名称'],
                ['seller_name', '商家名称'],
                ['store_avatar', '店铺头像'],
                ['store_label', '店铺LOGO'],
                ['grade_id', '店铺等级'],
                ['store_time', '开店时间'],
                ['create_time', '店铺分类'],
                ['create_time', '所在区域'],
                ['store_address', '详细地址'],
                ['store_state', '当前状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add','enable,disable,delete')// 批量添加顶部按钮
            ->addRightButtons('edit,delete')// 批量添加右侧按钮
            ->setRowList($data)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }


    /**
     * 店铺 待审核列表
     */
    public function store_joinin() {
        $model_store_joinin = Model('storejoinin');
        // 设置页码参数名称
        $condition = array();
        $condition['joinin_state'] = array('gt',0);
        if ($_POST['query'] != '') {
            $condition[$_POST['qtype']] = array('like', '%' . $_POST['query'] . '%');
        }
        $order = '';
        $param = array('member_id', 'member_name', 'sg_id', 'paying_amount', 'joinin_state', 'joinin_year', 'contacts_name', 'contacts_phone'
        ,'contacts_email', 'company_name', 'company_province_id', 'company_phone', 'company_employee_count', 'company_registered_capital','identity_id','identity_img'
        );
        if (in_array($_POST['sortname'], $param) && in_array($_POST['sortorder'], array('asc', 'desc'))) {
            $order = $_POST['sortname'] . ' ' . $_POST['sortorder'];
        }
        $page = $_POST['rp'];
        //店铺列表
        $store_list = $model_store_joinin->getList($condition, $page, $order);
        // 开店状态
        $joinin_state_array = $this->get_store_joinin_state();

      //  $data = array();
//        $data['now_page'] = $model_store_joinin->shownowpage();
//        $data['total_num'] = $model_store_joinin->gettotalnum();
        foreach ($store_list as $value) {
            $param = array();
//            if(in_array(intval($value['joinin_state']), array(STORE_JOIN_STATE_NEW, STORE_JOIN_STATE_PAY))) {
//                $operation = "<a class='btn orange' href=\"index.php?s=store&op=store_joinin_detail&member_id=". $value['member_id'] ."\"><i class=\"fa fa-check-circle\"></i>审核</a>";
//            } else {
//                $operation = "<a class='btn green' href=\"index.php?s=store&op=store_joinin_detail&member_id=". $value['member_id'] ."\"><i class=\"fa fa-list-alt\"></i>查看</a>";
//            }
         //   $param['operation'] = $operation;
            $param['member_id'] = $value['member_id'];
            $param['member_name'] = $value['member_name'];
            $param['sg_id'] = $value['sg_name'];
            $param['paying_amount'] = PriceFormat($value['paying_amount']);
            $param['joinin_state'] = $joinin_state_array[$value['joinin_state']];
            $param['joinin_year'] = $value['joinin_year'];
            $param['contacts_name'] = $value['contacts_name'];
            $param['contacts_phone'] = $value['contacts_phone'];
            $param['contacts_email'] = $value['contacts_email'];
            $param['company_name'] = $value['company_name'];
            $param['company_province_id'] = $value['company_address'] . ' ' . $value['company_address_detail'];
            $param['company_phone'] = $value['company_phone'];
            $param['company_employee_count'] = $value['company_employee_count'];
            $param['company_registered_capital'] = $value['company_registered_capital'];
            $param['identity_id'] = $value['identity_id'];
       //     $param['identity_img'] =  "<a href='javascript:void(0);' class='pic-thumb-tip' onMouseOut='toolTip()' onMouseOver='toolTip(\"<img src=".getShenFenZheng($value['identity_img'], 'store_logo').">\")'><i class='fa fa-picture-o'></i></a>";
            $data[$value['member_id']] = $param;
        }
        //var_dump($data);die;
        $list_tab = [
            'tab1' => ['title' => '入驻审核', 'url' => url('store_joinin')],
            'tab2' => ['title' => '管理', 'url' => url('index')],
        ];
        // 使用TKuitpl快速创建数据表格
        return TKuitpl::make('table')
            ->setPageTitle('供应商管理')// 设置页面标题
            ->setTabNav($list_tab,  'tab1')
            ->setPrimaryKey('goods_id') // 设置主键名为username
            ->setSearch(['member_id' => 'ID', 'member_name' => '会员名称'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['member_id', 'ID'],
                ['member_name', '会员名称'],
                ['contacts_email', '邮箱'],
                ['sg_id', '店铺等级'],
                ['company_name', '店铺名称'],
                ['joinin_state', '状态'],
                ['contacts_name', '联系人'],
                ['contacts_phone', '联系电话'],
                ['company_province_id', '详细地址'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add','enable,disable,delete')// 批量添加顶部按钮
            ->addRightButtons('edit,delete')// 批量添加右侧按钮
            ->setRowList($data)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }

    /**
     * 获得店铺状态
     *  open\正常
     *  close\关闭
     *  expire\即将到期
     *  expired\过期
     */
    private function getStoreState($store_info) {
        $result = 'open';
        if (intval($store_info['store_state']) === 1) {
            $store_end_time = intval($store_info['store_end_time']);
            if ($store_end_time > 0) {
                if ($store_end_time < TIMESTAMP) {
                    $result = 'expired';
                } elseif (($store_end_time - 864000) < TIMESTAMP) {
                    //距离到期10天
                    $result = 'expire';
                }
            }
        } else {
            $result = 'close';
        }
        return $result;
    }

    private function get_store_joinin_state() {
        $joinin_state_array = array(
            STORE_JOIN_STATE_NEW => '新申请',
            STORE_JOIN_STATE_PAY => '已付款',
            STORE_JOIN_STATE_VERIFY_SUCCESS => '待付款',
            STORE_JOIN_STATE_VERIFY_FAIL => '审核失败',
            STORE_JOIN_STATE_PAY_FAIL => '付款审核失败',
            STORE_JOIN_STATE_FINAL => '开店成功',
        );
        return $joinin_state_array;
    }
}


